package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import cn.com.cennavi.bean.DifPointTotalMapInfo;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.linemerge.LineMerger;

public class MergerLine {
	private static GeometryFactory factory = GeometryFactory.getInstance();
	private static double bufferAreaDistance = 0.0001;

	public static void main(String[] args) throws IOException, ParseException {
		LineMerger lineMerger = new LineMerger();
		List<Geometry> list = new ArrayList<Geometry>();
		list.add(factory.buildGeo("LINESTRING (3 3,2 2,0 0)"));
		list.add(factory.buildGeo("LINESTRING (3 3,6 6,0 10)"));
		list.add(factory.buildGeo("LINESTRING (0 10,3 1,10 1)"));
		lineMerger.add(list);
		Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings();
		for (Geometry g : mergerLineStrings) {
//			System.out.println(g.toText());
		}
		HashMap<String, Geometry> dataMap = getLinkGeoMap("d://data//R-G//R-G_16map.csv");
		getRoadGeo(dataMap, "d://data//DifPointTotalMapInfo.csv");
	}

	private static void getRoadGeo(HashMap<String, Geometry> dataMap, String string) throws ParseException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(string)));
		String line = "";
		while ((line = br.readLine()) != null) {
//			System.out.println(line);
			DifPointTotalMapInfo difPointTotalMapInfo = DifPointTotalMapInfo.parseData(line);
			String[] linkSeq = difPointTotalMapInfo.linkArr.split("\\|");
//			if (difPointTotalMapInfo.dif_Point_Id.equals("529217") && difPointTotalMapInfo.flag == 1) {
				LineMerger lineMerger = new LineMerger();
				List<Geometry> list = new ArrayList<Geometry>();
				StringBuffer gridIdsf=new StringBuffer();
				for (String link : linkSeq) {
					Geometry geo = dataMap.get(link);
					if (geo != null) {
						list.add(geo);
					}
				}
				lineMerger.add(list);
				Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings();
				for (Geometry dpr : mergerLineStrings) {
					Geometry g1 = new WKTReader().read(dpr.toString());
					Geometry buffer = g1.buffer(bufferAreaDistance);
//					System.out.println(buffer.toString());
					difPointTotalMapInfo.roadGeo=buffer;
					System.out.println(difPointTotalMapInfo.toString1());
				}
//			} 

		}
	}

	private static HashMap<String, Geometry> getLinkGeoMap(String string) throws IOException {
		// TODO Auto-generated method stub
		HashMap<String, Geometry> getLinkGeoMap = new HashMap<String, Geometry>();
		BufferedReader br = new BufferedReader(new FileReader(new File(string)));
		String line = "";
		while ((line = br.readLine()) != null) {
			String[] arr = line.split("\\:");
			String linkId = arr[0];
			Geometry geo = factory.buildGeo(arr[9]);
			getLinkGeoMap.put(linkId, geo);
		}
		return getLinkGeoMap;
	}
}
